﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AutoMapper;
using KelonProud.Administration.Dtos;
using KelonProud.Administration.Managers.Application;
using KelonProud.Administration.Managers.Authorization;
using KelonProud.Administration.Managers.MultiTenancy;
using KelonProud.Mvc;
using Microsoft.AspNetCore.Mvc;

namespace KelonProud.Administration.Controllers
{
    [AbpMvcAuthorize]
    [Route("admin/[controller]/[action]")]
    public class UserController : KelonProudControllerBase
    {
        private readonly TenantManager _tenantManager;
        private readonly RoleManager _roleManager;
        private readonly UserManager _userManager;

        public UserController(TenantManager tenantManager,
            RoleManager roleManager, UserManager userManager)
        {
            _tenantManager = tenantManager;
            _roleManager = roleManager;
            _userManager = userManager;
        }

        public ActionResult Index()
        {
            return View("KelonProud.Administration.Views.User.Index");
        }

        public ActionResult Create()
        {
            var model = new UserInputDto()
            {
                RoleDtos = _roleManager.Roles.ToList().Select(s => s.MapTo<RoleOutPutDto>())
            };

            return View("KelonProud.Administration.Views.User.Edit", model);
        }

        public ActionResult Edit(int id)
        {
            var nav = _userManager.GetById(id);

            if (nav == null)
                throw new Exception("需要编辑的用户不存在!");

            var model = nav.MapTo<UserInputDto>();

        

            model.RoleDtos = _roleManager.Roles.ToList().Select(s => s.MapTo<RoleOutPutDto>());
       
            model.RoleIds = nav.Roles.Select(s => s.RoleId.ToString()).ToArray();

            return View("KelonProud.Administration.Views.User.Edit",model);
        }

        public ActionResult ModifyPwd()
        {
            return View("KelonProud.Administration.Views.User.ModifyPwd",new UserModifyPwdInPut()); 
        }
    }
}
